package cn.itcast.mq.listener;

import org.junit.jupiter.api.Test;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 1.注册IOC容器
 * 2.写一个监听方法，使用注解标记为MQ监听
 * 3. 方法的参数就是消息
 */
@Component
public class SpringRabbitListener {
//    @RabbitListener(queues = "simple.queue")
//    public void listenerSimpleQueue1(String message) throws InterruptedException {
//        System.out.println("消费者A:"+message);
//        // 消费快
//        Thread.sleep(10);
//    }
//    @RabbitListener(queues = "simple.queue")
//    public void listenerSimpleQueue2(String message) throws InterruptedException {
//        System.out.println("消费者B:"+message);
//        // 消费慢
//        Thread.sleep(30);
//    }

    @RabbitListener(queues = "fanout.order.queue")
    public void listenerOrderQueue(String message) throws InterruptedException {
        System.out.println("订单消费者 订阅消息:"+message);
    }

    @RabbitListener(queues = "fanout.sms.queue")
    public void listenerSmsQueue(String message) throws InterruptedException {
        System.out.println("短信消费者 订阅消息:"+message);
    }

    // 定义监听方法的同时声明队列、交换机及路由key
    @RabbitListener(bindings = @QueueBinding(
        value = @Queue(name = "direct.order.queue"),
         exchange = @Exchange(name = "pay.direct",type = ExchangeTypes.DIRECT),
         key = {"success","close"}
     )
    )
    public void listenerDirectOrderQueue(String message){
        System.out.println("订单业务，消费消息:"+message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.sms.queue"),
            exchange = @Exchange(name = "pay.direct",type = ExchangeTypes.DIRECT),
            key = {"success","refund"}
    ))
    public void listenerDirectSmsQueue(String message){
        System.out.println("短信业务，消费消息:"+message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.info.queue"),
            exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
            key = {"info.log"}
    ))
    public void listenerInfoQueue(String message){
        System.out.println("日志收集，收集普通日志:"+message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.error.queue"),
            exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
            key = {"error.log"}
    ))
    public void listenerErrorQueue(String message){
        System.out.println("日志收集，收集错误日志:"+message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.warning.queue"),
            exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
            key = {"warning.log"}
    ))
    public void listenerWarningQueue(String message){
        System.out.println("日志收集，收集警告日志:"+message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.all.queue"),
            exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
            key = {"#.log"}
    ))
    public void listenerAllQueue(String message){
        System.out.println("日志收集，收集所有日志:"+message);
    }

    @RabbitListener(queues = "object.queue")
    public void listenerObjectMessage(Map<String,Object> msg){
        System.out.println(msg);
    }


}
